/**
 * A sample implementation of the html page gen component.
 * You can use it as is or customize it.
 */

import java.io.*;
import java.util.*;

public class HTMLPageGen {
  private String outDir;  // path to directory in which to generate html files

  private ArrayList<String> imageList;          // array of image file names
  private ArrayList<String[]> commentList;  // corresponding array of array of comments

  // constructor doesn't do much
  public HTMLPageGen(String outputDir) {
    outDir = outputDir;
    imageList = new ArrayList<String>();
    commentList = new ArrayList<String[]>();
  }

  // register a photo file and an array of comments for it
  public void addPhotoAndComments( String photoFile, String[] comments) {
    String[] commentCopy = new String[ comments.length ];
    System.arraycopy( comments, 0, commentCopy, 0, comments.length );
    imageList.add( photoFile );
    commentList.add( commentCopy );
  }

  // actually create the pages
  public void createPages() throws IOException {
    createIndexPage();
    createNavPage();
    createPhotoPages();
  }

  // create index.html
  private void createIndexPage() throws IOException {
    BufferedWriter outfile = new BufferedWriter( new FileWriter(outDir + System.getProperty("file.separator") + "index.html" ) );
    appendFile( "index.inc", outfile );
    outfile.close();
  }

  // create nav.html
  private void createNavPage() throws IOException {
    BufferedWriter outfile = new BufferedWriter( new FileWriter(outDir + System.getProperty("file.separator") + "nav.html" ) );
    appendFile( "nav.head", outfile );
    // dump javascript method invocation per image file
    for ( int i=0; i<imageList.size(); i++ ) {
      File imageFile = new File( imageList.get(i) );
      String imageFilename = imageFile.getName();
      String htmlFilename = imageFilename.substring(0, imageFilename.lastIndexOf(".")) + ".html";
      outfile.write( "createNavElements( '" + imageFile.getAbsolutePath() + "' ,'" + htmlFilename + "', " + commentList.get(i).length + " );\n" );
    }
    appendFile( "nav.tail", outfile );
    outfile.close();
  }

  // create individual photo content pages
  private void createPhotoPages() throws IOException {
    for ( int i=0; i<imageList.size(); i++ ) {
      File imageFile = new File( imageList.get(i) );
      String imageFilename = imageFile.getName();
      String htmlFilename = outDir + System.getProperty("file.separator") + imageFilename.substring(0, imageFilename.lastIndexOf(".")) + ".html";
      BufferedWriter outfile = new BufferedWriter( new FileWriter(htmlFilename ) );
      appendFile( "content.head", outfile);

      outfile.write("<img src='" + imageFile.getAbsolutePath() + "'>\n");      outfile.write( "<div style='width:800'><p>\n" );
      for ( int c=0; c<commentList.get(i).length; c++ ) {
        outfile.write( commentList.get(i)[c] + "\n<hr><p>\n" );
      }
      outfile.write("</div>\n");
      outfile.append( "content.tail" );
      outfile.close();
    }
  }

  // copy a file into the output BufferedWriter
  private void appendFile ( String filename, BufferedWriter f ) throws IOException {
    BufferedReader infile = new BufferedReader( new FileReader(filename) );
    String nextLine = null;
    while ( (nextLine = infile.readLine()) != null ) {
      f.write( nextLine + "\n" );
    }
    infile.close();
  }


  // main() shows how to use this class

  public static void main(String args[]) throws IOException {
    HTMLPageGen gen = new HTMLPageGen( "../../htdocs");

    String[] comments = new String[2];
    comments[0] = " This is an example comment.  It's not intended to indicate\n" +
      "<i>exactly</i> how you should list comments - that's completely\n" +
      "up to you.  It's just here to show a bit more html formatting,\n" +
      "in case you're not familiar with html.\n";
    comments[1] = "Another comment";

    // the image file path can be absolute or relative to whatever the current working directory
    // is when this code runs
    gen.addPhotoAndComments( "../../images/2594806384_fab8877494_b.jpg", comments);
    gen.addPhotoAndComments( "../../images/3697893298_9696c04dc5_b.jpg", comments);
    gen.addPhotoAndComments( "../../images/3855919965_f5e1307e07.jpg", comments);

    gen.createPages();
    
  }

}